package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmSenstitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


@SuppressWarnings("ALL")
@Service
@Slf4j
@Transactional
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {

    /*
     * 查询敏感词列表
     * */
    @Override
    public ResponseResult selectSensitive(WmSenstitiveDto dto) {

        //1.效验参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Page page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
        //按照创建时间倒序
        //按照名称模糊查询
        queryWrapper.like(dto.getName() != null, WmSensitive::getSensitives, dto.getName());
        // queryWrapper.eq(dto.getName() != null, WmSensitive::getSensitives, dto.getName());
        //5.根据创建日期倒序
        queryWrapper.orderByDesc(WmSensitive::getCreatedTime);
        //分页查询
        page = page(page, queryWrapper);
        //3.查询完毕，完成结果封装；
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        //4.响应结果
        return responseResult;
    }
    /*
     * 新增敏感词
     * */

    @Override
    public ResponseResult saveSensitive(WmSensitive wmSensitive) {
        //效验参数
        if (wmSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        }
        //敏感词存在，不能保存
        WmSensitive sensitive = getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, wmSensitive.getSensitives()));
        if (sensitive != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "敏感词已存在");
        }
        wmSensitive.setCreatedTime(new Date());
        save(wmSensitive);
        //封装结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /*
     * 敏感词修改
     * */

    @Override
    public ResponseResult updateSensitive(WmSensitive wmSensitive) {
        if (wmSensitive == null || wmSensitive.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        updateById(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
    /*
     * 删除敏感词，根据id
     * */


    @Override
    public ResponseResult deleteSensitive(Integer id) {
        if (id == null && id.equals("")) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmSensitive wmSensitive = getById(id);
        if (wmSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //删除id
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
